package com.jcl.core.security;

import com.jcl.core.utils.EncryptUtils;

/**
 * @ClassName ShareUserToken
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/25 14:55
 * @Version 1.0
 **/
public class ShareUserToken {
    private static final String split = "#";
    public static final String search_from = "0";
    public static final String resume_from = "1";
    private String corpCode;
    private String userId;
    private String fromType;
    private String resumeId;
    private String applyId;

    public static void main(String[] args) {
        ShareUserToken info = decodeInviteToken("C1CBED6C4E7717BAFEF0C7D80AFECAAD");
        System.out.println(info);

    }

    public static ShareUserToken decodeResumeToken(String token){
        ShareUserToken info = null;
        String vStr = null;
        try {
            vStr = EncryptUtils.simpleDecode(token);
            String[] arr = vStr.split(split);
            info = newInstance();
            info.setCorpCode(arr[0]);
            info.setUserId(arr[1]);
            info.setResumeId(arr[2]);
            info.setFromType(arr[3]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    public static ShareUserToken decodeInviteToken(String token){
        return decodeUserToken(token);
    }

    public static ShareUserToken decodeUserToken(String token){
        ShareUserToken info = null;
        String vStr = null;
        try {
            vStr = EncryptUtils.simpleDecode(token);
            String[] arr = vStr.split(split);
            info = newInstance();
            info.setUserId(arr[1]);
            info.setCorpCode(arr[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return info;
    }

    public static String encodeResumeToken(String corpCode,
                                           String userId,
                                           String resumeId,
                                           String fromType){
        ShareUserToken info = newInstance();
        info.setCorpCode(corpCode);
        info.setUserId(userId);
        info.setResumeId(resumeId);
        info.setFromType(fromType);
        return EncryptUtils.simpleEncode(info.toResString());
    }

    public static String encodeInviteToken(String corpCode, String inviteUserId){
        ShareUserToken info = newInstance();
        info.setCorpCode(corpCode);
        info.setUserId(inviteUserId);
        return EncryptUtils.simpleEncode(info.toInviteString());
    }

    public static String encodeUserToken(String corpCode,
                                         String userId){
        ShareUserToken info = newInstance();
        info.setCorpCode(corpCode);
        info.setUserId(userId);
        return EncryptUtils.simpleEncode(info.toUserString());
    }

    public static ShareUserToken newInstance(){
        return new ShareUserToken();
    }

    public String getCorpCode() {
        return corpCode;
    }

    public void setCorpCode(String corpCode) {
        this.corpCode = corpCode;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getFromType() {
        return fromType;
    }

    public void setFromType(String fromType) {
        this.fromType = fromType;
    }

    public String getResumeId() {
        return resumeId;
    }

    public void setResumeId(String resumeId) {
        this.resumeId = resumeId;
    }

    public String getApplyId() {
        return applyId;
    }

    public void setApplyId(String applyId) {
        this.applyId = applyId;
    }

    public String toResString() {
        return corpCode + split + userId + split + resumeId + split + fromType;
    }

    public String toInviteString() {
        return corpCode + split + userId + split;
    }
    public String toUserString() {
        return corpCode + split + userId;
    }
}
